package com.yqy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqy.entity.GradeInfo;
import com.yqy.entity.MessageBoard;
import com.yqy.entity.StudentGrade;
import com.yqy.enums.ProjectBeLone;
import com.yqy.mapper.GradeInfoMapper;
import com.yqy.mapper.StudentGradeMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yiqianyi
 * @date 2022/3/23 9:24
 */
@RestController
@CrossOrigin
@Api(tags = {"终测成绩明细（加减分）控制层"})
public class GradeInfoController {
    @Autowired
    private GradeInfoMapper gradeInfoMapper;

    @Autowired
    private StudentGradeMapper studentGradeMapper;

    @PostMapping("/grade/info/insert")
    @ApiOperation(value = "插入学生成绩明细", notes = "/grade/info/insert")
    public int insertGradeInfo(@RequestBody @ApiParam(name = "GradeInfo", value = "GradeInfo", required = true) GradeInfo gradeInfo) {
        QueryWrapper<StudentGrade> queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", gradeInfo.getUsername());
        List<StudentGrade> studentGradeList = studentGradeMapper.selectList(queryWrapper);
        StudentGrade studentGrade = studentGradeList.get(0);
        if (ProjectBeLone.WT.getName().equals(gradeInfo.getProjectBelong())) {
            int wtPs = studentGrade.getWtPsScore();
            studentGrade.setWtPsScore(wtPs + gradeInfo.getScore());
        } else if (ProjectBeLone.ZY.getName().equals(gradeInfo.getProjectBelong())) {
            int zyPs = studentGrade.getZyPsScore();
            studentGrade.setZyPsScore(zyPs + gradeInfo.getScore());
        } else if (ProjectBeLone.DY.getName().equals(gradeInfo.getProjectBelong())) {
            int dyPs = studentGrade.getDyPsScore();
            studentGrade.setDyPsScore(dyPs + gradeInfo.getScore());
        }
        int ps = studentGrade.getWtPsScore() + studentGrade.getZyPsScore() + studentGrade.getDyPsScore();
        int qz = studentGrade.getWtQzScore() + studentGrade.getZyQzScore() + studentGrade.getDyQzScore();
        Double sum = ps * 0.3 + qz * 0.7;
        studentGrade.setSumScore((double) (Math.round(sum * 100) / 100.0));
        studentGradeMapper.updateById(studentGrade);
        return gradeInfoMapper.insert(gradeInfo);
    }

    @PostMapping("/grade/info/getPage")
    @ApiOperation(value = "分页获取个人成绩明细", notes = "/grade/info/getPage")
    public IPage<MessageBoard> getPageStudentInfo(@RequestParam @ApiParam(name = "pageNum", value = "pageNum", required = true) int pageNum, @RequestParam @ApiParam(name = "pageSize", value = "pageSzie", required = true) int pageSize, @RequestBody @ApiParam(name = "GradeInfo", value = "GradeInfo", required = true) GradeInfo gradeInfo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (!("").equals(gradeInfo.getUsername())) {
            queryWrapper.eq("username", gradeInfo.getUsername());
        }
        if (!("").equals(gradeInfo.getProjectBelong())) {
            queryWrapper.eq("projectBelong", gradeInfo.getProjectBelong());
        }
        Page<GradeInfo> gradeInfoPage = new Page<>(pageNum, pageSize);
        return gradeInfoMapper.selectPage(gradeInfoPage, queryWrapper);
    }
}
